{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "63743bac-a28c-4e5a-b085-fa3b59ac45a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入题目编号（1-19）： 13\n",
      "请输入一个奇数N： 19\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "***\n",
      "*****\n",
      "*******\n",
      "*********\n",
      "***********\n",
      "*************\n",
      "***************\n",
      "*****************\n",
      "*******************\n",
      "*****************\n",
      "***************\n",
      "*************\n",
      "***********\n",
      "*********\n",
      "*******\n",
      "*****\n",
      "***\n",
      "*\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "# 1. 输入某年某月某日，判断这一天是这一年的第几天\n",
    "def day_of_year(year, month, day):\n",
    "    date = datetime(year, month, day)\n",
    "    return (date - datetime(year, 1, 1)).days + 1\n",
    "\n",
    "# 3. 判断1000中有多少个素数，并输出所有素数\n",
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    if n <= 3:\n",
    "        return True\n",
    "    if n % 2 == 0 or n % 3 == 0:\n",
    "        return False\n",
    "    i = 5\n",
    "    while i * i <= n:\n",
    "        if n % i == 0 or n % (i + 2) == 0:\n",
    "            return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "# 5. 利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示， 60-89分之间的用B表示， 60分以下的用C表示\n",
    "def grade(score):\n",
    "    if score >= 90:\n",
    "        return 'A'\n",
    "    elif score >= 60:\n",
    "        return 'B'\n",
    "    else:\n",
    "        return 'C'\n",
    "\n",
    "# 7. 输入一行字符串， 分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "def count_characters(input_str):\n",
    "    letter_count = 0\n",
    "    space_count = 0\n",
    "    digit_count = 0\n",
    "    other_count = 0\n",
    "\n",
    "    for char in input_str:\n",
    "        if char.isalpha():\n",
    "            letter_count += 1\n",
    "        elif char.isspace():\n",
    "            space_count += 1\n",
    "        elif char.isdigit():\n",
    "            digit_count += 1\n",
    "        else:\n",
    "            other_count += 1\n",
    "\n",
    "    return letter_count, space_count, digit_count, other_count\n",
    "\n",
    "# 9. 一个数如果恰好等于它的因子之和， 这个数就称为“完数”。例如6=1+2+3.编程， 找出1000以内的所有完数\n",
    "def find_perfect_numbers(limit):\n",
    "    perfect_numbers = []\n",
    "    for num in range(1, limit):\n",
    "        sum_of_factors = 0\n",
    "        for i in range(1, num):\n",
    "            if num % i == 0:\n",
    "                sum_of_factors += i\n",
    "        if sum_of_factors == num:\n",
    "            perfect_numbers.append(num)\n",
    "    return perfect_numbers\n",
    "\n",
    "# 11. 输入任意数N， 求1+2!+3!+…+N!！的值\n",
    "def factorial(n):\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n-1)\n",
    "\n",
    "# 13. 输入任意奇数N， 按如下要求打印出图案 (菱形)\n",
    "def print_diamond(N):\n",
    "    # 打印上半部分包括中间行\n",
    "    for i in range(1, N + 1, 2):\n",
    "        print('*' * i)\n",
    "\n",
    "    # 打印下半部分\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        print('*' * i)\n",
    "\n",
    "# 15. 对10个数进行排序\n",
    "def sort_numbers():\n",
    "    numbers = [int(x) for x in input(\"请输入10个数，用空格分隔：\").split()]\n",
    "    numbers.sort()\n",
    "    return numbers\n",
    "\n",
    "# 17. 有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中\n",
    "def insert_into_sorted_array(sorted_array, new_number):\n",
    "    insert_position = 0\n",
    "    for i in range(len(sorted_array)):\n",
    "        if sorted_array[i] > new_number:\n",
    "            insert_position = i\n",
    "            break\n",
    "    else:\n",
    "        insert_position = len(sorted_array)\n",
    "\n",
    "    sorted_array.insert(insert_position, new_number)\n",
    "    return sorted_array\n",
    "\n",
    "# 19. 有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位\n",
    "def last_remaining(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)  \n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "\n",
    "# 主函数，根据用户输入选择运行哪个问题\n",
    "def main():\n",
    "    problem_number = int(input(\"请输入题目编号（1-19）：\"))\n",
    "    \n",
    "    if problem_number == 1:\n",
    "        year = int(input(\"请输入年份: \"))\n",
    "        month = int(input(\"请输入月份: \"))\n",
    "        day = int(input(\"请输入日期: \"))\n",
    "        print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year(year, month, day)} 天。\")\n",
    "    elif problem_number == 3:\n",
    "        primes = []\n",
    "        for num in range(2, 1001):\n",
    "            if is_prime(num):\n",
    "                primes.append(num)\n",
    "        prime_count = len(primes)\n",
    "        print(prime_count)\n",
    "        print(primes)\n",
    "    elif problem_number == 5:\n",
    "        score = float(input(\"请输入学生成绩：\"))\n",
    "        grade = grade(score)\n",
    "        print(grade)\n",
    "    elif problem_number == 7:\n",
    "        input_str = input(\"请输入一行字符串：\")\n",
    "        letter_count, space_count, digit_count, other_count = count_characters(input_str)\n",
    "        print(\"英文字母个数：\", letter_count)\n",
    "        print(\"空格个数：\", space_count)\n",
    "        print(\"数字个数：\", digit_count)\n",
    "        print(\"其他字符个数：\", other_count)\n",
    "    elif problem_number == 9:\n",
    "        perfect_numbers = find_perfect_numbers(1000)\n",
    "        print(\"1000以内的完数有：\", perfect_numbers)\n",
    "    elif problem_number == 11:\n",
    "        N = int(input(\"请输入任意数N：\"))\n",
    "        sum_of_factorials = sum(factorial(i) for i in range(1, N+1))\n",
    "        print(f\"1+2!+3!+…+{N}!的值为：{sum_of_factorials}\")\n",
    "    elif problem_number == 13:\n",
    "        N = int(input(\"请输入一个奇数N：\"))\n",
    "        print_diamond(N)\n",
    "    elif problem_number == 15:\n",
    "        numbers = sort_numbers()\n",
    "        print(\"排序后的数列为：\", numbers)\n",
    "    elif problem_number == 17:\n",
    "        sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "        new_number = int(input(\"请输入一个数：\"))\n",
    "        sorted_array = insert_into_sorted_array(sorted_array, new_number)\n",
    "        print(\"插入新数后的数组为：\", sorted_array)\n",
    "    elif problem_number == 19:\n",
    "        n = int(input(\"请输入人数n：\"))\n",
    "        print(\"最后留下的是原来第{}号的那位。\".format(last_remaining(n)))\n",
    "    else:\n",
    "        print(\"没有这个编号的题目\")\n",
    "\n",
    "# 调用主函数\n",
    "main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
